package me.zhengjie.modules.system.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.read.listener.ReadListener;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import me.zhengjie.constant.ConstantUtils;
import me.zhengjie.modules.system.domain.CourseClass;
import me.zhengjie.modules.system.domain.CourseClassStudent;
import me.zhengjie.modules.system.domain.vo.CourseClassStudentVo;
import me.zhengjie.modules.system.service.CourseClassStudentService;
import me.zhengjie.modules.system.service.ExamInfoService;
import me.zhengjie.modules.web.service.mybatisplus.ICourseClassPlusService;
import me.zhengjie.modules.web.service.mybatisplus.ICourseClassStudentPlusService;
import me.zhengjie.utils.DateUtil;
import me.zhengjie.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Objects;

/**
 * @author CN-xianchengzhao
 * @description: TODO
 * @date 2021/11/21 12:14
 */
@Component
public class UploadClassStudentListener implements ReadListener<CourseClassStudentVo> {

    public static ThreadLocal<String> atmrLocal = new ThreadLocal<>();

    @Autowired
    ExamInfoService examInfoService;

    @Autowired
    ICourseClassStudentPlusService classStudentPlusService;

    @Autowired
    ICourseClassPlusService classPlusService;

   /* @Autowired
    private CourseClassStudentService courseClassStudentService;
    */
    private static final Logger log = LoggerFactory.getLogger(UploadClassStudentListener.class);
    @Override
    public void invoke(CourseClassStudentVo studentVo, AnalysisContext analysisContext) {
        if (StringUtils.isAnyBlank(studentVo.getMobile(),studentVo.getRealName(),studentVo.getChannelSource(),studentVo.getGroupNo())) {
            return;
        }
        LambdaQueryWrapper<CourseClass> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CourseClass::getClassNo,atmrLocal.get());
        CourseClass one = classPlusService.getOne(wrapper);
        if (Objects.isNull(one)){
            return;
        }
        log.info("==============================================studentVo="+ JSON.toJSONString(studentVo));
        /*LambdaQueryWrapper<CourseClassStudent> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(CourseClassStudent::getPhoneNo,studentVo.getMobile());
        wrapper1.eq(CourseClassStudent::getClassNo,atmrLocal.get());
        List<CourseClassStudent> list = classStudentPlusService.list(wrapper1);
        if (!CollectionUtils.isEmpty(list)){
            return;
        }*/
        CourseClassStudent student = new CourseClassStudent();
        student.setClassNo(one.getClassNo());
        student.setCourseNo(one.getCourseNo());
        student.setPhoneNo(studentVo.getMobile());
        student.setUserName(studentVo.getRealName());
        /*String meals = studentVo.getMeals();
        int paymeals = 0;
        if ("是".equals(meals)){
            paymeals = 1;
        }*/
        //默认已支付
        student.setMealsStatus(ConstantUtils.Meals_Payed);
        student.setGroupNumber(studentVo.getGroupNo());
        student.setCreateTime(DateUtil.TimestampNow());
        student.setChannelSource(studentVo.getChannelSource());
        log.info("==============================================student="+ JSON.toJSONString(student));
        try {
            classStudentPlusService.saveOrUpdate(student);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //courseClassStudentService.create(student);
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }

}